// Modules to control application life and create native browser window
const {app, BrowserWindow, Menu } = require('electron')
const path = require('path')
const url  = require('url')
const winApp = require('./app/index').default

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow
// TODO 隐藏顶部菜单
Menu.setApplicationMenu(null);
let isModel = true;

try{
  let argv = global.process.argv[2];
  if(argv.indexOf('dev') >= 0) {
    isModel = false
  }
}catch (e) {}


function createWindow () {
  // Create the browser window.
  mainWindow = new BrowserWindow({
    width: 1237,
    height: 780,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js'),
      javascript: true,
      plugins: true,
      nodeIntegration: true, // 是否集成 Nodejs
      webSecurity: false,
    }
  })
  // and load the index.html of the app.
  // mainWindow.loadFile('index.html')
  if (isModel) {
    app.model = 'file';
    mainWindow.loadURL(url.format({
      pathname: path.join(__dirname, './build/index.html'),
      protocol: 'file:',
      slashes: true
    }))
  } else {
    app.model = 'local';
    mainWindow.loadURL('http://localhost:3000/')
    // TODO 打开控制台
    mainWindow.webContents.openDevTools()
  }
  mainWindow.on('closed', function () {
    // Dereference the window object, usually you would store windows
    // in an array if your app supports multi windows, this is the time
    // when you should delete the corresponding element.
    mainWindow = null
  })
  winApp.init(mainWindow, app)
}
app.on('ready', createWindow)
app.on('window-all-closed', function () {
  if (process.platform !== 'darwin') app.quit()
})
app.on('activate', function () {
  if (mainWindow === null) createWindow()
})
